// This is a manifest file that'll be compiled into application.js, which will include all the files
// listed below.
//
// Any JavaScript/Coffee file within this directory, lib/assets/javascripts, vendor/assets/javascripts,
// or vendor/assets/javascripts of plugins, if any, can be referenced here using a relative path.
//
// It's not advisable to add code directly here, but if you do, it'll appear at the bottom of the
// the compiled file.
//
// WARNING: THE FIRST BLANK LINE MARKS THE END OF WHAT'S TO BE PROCESSED, ANY BLANK LINE SHOULD
// GO AFTER THE REQUIRES BELOW.
//
//= require jquery
//= require jquery_ujs
//= require jstree
//= require ckeditor/init
//= require_tree .

var util = {
    isUndefined: function (x) {
        return (x == undefined);
    },

    createElement: function (tag, name, type, value) {
        if (this.isUndefined(tag))
            throw new Error("Tag must not be undefined");

        var e = document.createElement(tag);

        if (!this.isUndefined(name)) {
            e.id = name;
            e.name = name;
        }

        if (!this.isUndefined(type))
            e.type = type;

        if (!this.isUndefined(value))
            e.value = value;

        return e;
    },

    createInputTextLine: function (textfield_id, title, type, value) {
        type = type || "input";

        var e = util.createElement("div");

        var t = util.createElement("label");
        t.innerHTML = title;
        e.appendChild(t);

        e.appendChild(util.createElement(type, textfield_id, "text", value));

        return e;
    },

    createCheckbox: function (field_id, title1, type, value) {
        type = type || "input";

        var e = util.createElement("span");

        var t = util.createElement("span");
        t.innerHTML = title1;

        e.appendChild(t);
        e.appendChild(util.createElement(type, field_id, "checkbox", value));

        return e;
    },

    nodeMoveToEnd: function(node) {
        if ( node == undefined)
            throw new Error("Parameter must not be undefined");

        var parent = node.parentNode;
        parent.removeChild(node);
        parent.appendChild(node);
    }
};

function closeResultWindow(w) {
    if ( w == null )
        return;

    w.parentNode.removeChild(w);

    closeResultWindow(document.getElementById(w.id));

    //toggleSeachingWindows(document.getElementById("search_window_div"));
}

function printResults(data)
{
    var results = document.createElement("div");
    $("body").append(results);

    results.id = "searchresults";
    results.className = "searchresults";

    results.innerHTML = data;

    results.onclick = function() {
        closeResultWindow(this);
    };
}

function startExtendedSearch() {
    var searchValue = {
        title : document.getElementById("title_input_text").value,
        chunkTitle : document.getElementById("chunk_title_input_text").value,
        author : document.getElementById("autor_input_text").value,
        year : document.getElementById("year_input_text").value,
        genre : document.getElementById("genre_input_text").value,
        tags : document.getElementById("tags_input_text").value,
        combineByOr : document.getElementById("combination_checkbox").checked
    };

    //console.log(searchValue);
    $.get("http://localhost:3000/books/searchext", searchValue, function(new_data) {
        printResults(new_data);
    });
}

function startSimpleSearch() {
    var searchValue = document.getElementById("search_input_text").value;

    //console.log(searchValue);
    $.get("http://localhost:3000/books/search", { 'keyword': searchValue }, function (new_data) {
        printResults(new_data);
    });
}

function extendSearch(callingElement) {
    var simpleForm = callingElement.parentNode;

    var searchElement = simpleForm.parentNode;
    searchElement.removeChild(simpleForm);

    appendExtendedSearch(searchElement);
}

function minimizeSearch(callingElement) {
    var extendedForm = callingElement.parentNode;

    var searchElement = extendedForm.parentNode;
    searchElement.removeChild(extendedForm);

    addSimpleSearch(searchElement);
}

function appendExtendedSearch(parent) {
    var form = util.createElement("form", "extended_search_form");

    var search = util.createElement("input", "extended_search", "button", "Suche");
    search.onclick=function(){startExtendedSearch(this);};
    form.appendChild(search);

    var minimize = util.createElement("input", "minimize_search", "button", "Einfach");
    minimize.onclick=function(){minimizeSearch(this);};
    form.appendChild(minimize);

    form.appendChild(util.createElement("br"));

    form.appendChild(util.createInputTextLine("title_input_text", "Name "));
    form.appendChild(util.createInputTextLine("chunk_title_input_text", "Kapitelname "));
    form.appendChild(util.createInputTextLine("autor_input_text", "Autor "));
    form.appendChild(util.createInputTextLine("year_input_text", "Jahr "));
    form.appendChild(util.createInputTextLine("genre_input_text", "Genre "));
    form.appendChild(util.createInputTextLine("tags_input_text", "Tags "));
    form.appendChild(util.createElement("br"));
    form.appendChild(util.createCheckbox("combination_checkbox", "Oder "));

    parent.appendChild(form)
}

function addSimpleSearch(elem)
{
    elem.appendChild(util.createElement("h2", null, null, "Suche"));

    var form = util.createElement("form", "simple_search");

    var input = util.createElement("input", "search_input_text", "text", "Suche");
    input.onfocus = function(){document.getElementById("search_input_text").value = "";};
    input.onblur = function() {
        var el = document.getElementById("search_input_text");
        if ( el.value === "" )
            el.value = "Suche";
    };

    var suche = util.createElement("input", "start_searching_button", "button", "Suche");
    suche.onclick = function(){startSimpleSearch(this);};

    var extend = util.createElement("input", "extend_search", "button", "Erweitert");
    extend.onclick = function(){extendSearch(this);};

    form.appendChild(suche);
    form.appendChild(extend);
    form.appendChild(util.createElement("br"));
    form.appendChild(input);

    elem.appendChild(form);
}

var isSearchOpened = false;
function toggleSeachingWindows(elem, button) {
    button = button || document.getElementById("showSearch");
    if (isSearchOpened) {
        elem.hidden = true;
        button.innerHTML = "Öffne Suche";
    }
    else {
        elem.hidden = false;
        button.innerHTML = "Schließe Suche";
    }
    isSearchOpened = !isSearchOpened;
}
